import { getExpiryTime } from '@/utils';
import aepApiRequest from '../utils/aepApiRequest';

// interface ShareDevice {
//   deviceName: string;
//   shareNumber: number;
//   sno: string;
//   mac: string;
//   productKey: string;
// }

// interface ShareRoom {
//   roomId: number;
//   roomName: string;
//   shareDeviceList: ShareDevice[]
// }

export interface IShareUser {
  userId: number;
  avatar: string;
  nickname: string;
  mobile: string;
  shareTime: string;
  email: string;
}

// 我分享的设备
export interface IMyShareDevice {
  name: string;
  count: number;
  sno: string;
  mac: string;
  productKey: string;
}

// 分享给我的设备
export interface ISharedDevice {
  deviceName: string;
  count: number;
  sno: string;
  mac: string;
  productKey: string;
  ctime: string;
  username: string;
  utime: string;
}


// export async function fetchShareDevices(type: 1 | 2) {
//   return aepApiRequest<ShareRoom[]>(`app/smart_home/homes/shareDeviceList?type=${type}`, {
//     method: 'GET',
//   });
// }

// 分享给我的设备列表
export async function fetchShareToMineDevices() {
  return aepApiRequest<ISharedDevice[]>('app/smartHome/homes/shareDeviceListV2', {
    method: 'GET',
  });
}

// 我分享的设备列表
export async function fetchMyShareDevices() {
  return aepApiRequest<IMyShareDevice[]>('app/smartHome/homes/deviceListSingle', {
    method: 'GET',
  });
}

export async function fetchDeviceShareUsers(mac: string, productKey: string) {
  return aepApiRequest<IShareUser[]>(
    `app/smart_home/homes/singleDeviceShareUserList?mac=${mac}&productKey=${productKey}`,
    { method: 'GET' }
  );
}

type CreateShareData = {
  shareDevices: Pick<IDevice, 'mac' | 'productKey'>[];
  numberLimit?: number;
  expiryDays?: number;
  expiryTime?: string;
};

export async function createDeviceShareRecord({
  shareDevices,
  expiryTime,
  numberLimit = 1,
  expiryDays = 1,
}: CreateShareData) {
  const endTime = expiryTime || getExpiryTime(expiryDays);
  return aepApiRequest<string>('app/smart_home/homes/addSingleDeviceShareLog', {
    method: 'POST',
    body: {
      shareDevices,
      numberLimit,
      endTime,
    },
  });
}

export async function updateDeviceShareRecord({
  code,
  expiryTime,
  numberLimit = 1,
  expiryDays = 1,
}: Omit<CreateShareData, 'shareDevices'> & { code: string }) {
  const expireTime = expiryTime || getExpiryTime(expiryDays);
  return aepApiRequest<string>('app/smart_home/homes/updateSingleDeviceLog', {
    method: 'POST',
    body: {
      code,
      limitNumber: numberLimit,
      expireTime,
    },
  });
}

export async function fetchDeviceShareRecord(code: string) {
  return aepApiRequest<string>(`app/smart_home/homes/singleDeviceLogDetails/${code}`, {
    method: 'GET',
  });
}

export async function acceptDeviceShare(code: string) {
  return aepApiRequest<IShareUser[]>(
    `app/smartHome/homes/singleDeviceShareV2/${code}`,
    {
      method: 'POST',
    },
  )
}

export async function cancelDeviceShare(data: { mac: string; productKey: string }[]) {
  return aepApiRequest<IShareUser[]>(
    'app/smart_home/homes/deviceStopShare',
    {
      method: 'POST',
      body: data.map(({ mac, productKey }) => ({ mac, productKey }))
    },
  )
}

export async function cancelDeviceShareToUsers(body: { mac: string; productKey: string; userIds: number[] }) {
  return aepApiRequest<boolean>('app/smart_home/homes/deviceStopUserShare', {
    method: 'POST',
    body,
  });
}

export async function removeReceiveShareDevices(data: { mac: string; productKey: string }[]) {
  return aepApiRequest<boolean>('app/smart_home/homes/userEndDeviceShare', {
    method: 'POST',
    body: data.map(({ mac, productKey }) => ({ mac, productKey })),
  });
}

// 接受单设备分享
export async function acceptShareDevicesNoti(id: string) {
  return aepApiRequest('app/smartHome/homes/acceptSingleDevice', {
    method: 'POST',
    body: id,
  });
}

// 发送单设备（标准）分享通知
export async function sendShareDevicesNoti({ data, account }:
  { account: string, data: { mac: string; productKey: string }[] }) {
  const encodeAccount = encodeURIComponent(account);
  return aepApiRequest(
    `app/smartHome/homes/sendSingleDeviceShareNotice?account=${encodeAccount}`,
    {
      method: 'POST',
      body: data,
    },
  );
}

